package com.stars.empoly.widget

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.Gravity
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.stars.empoly.R
import com.stars.empoly.databinding.WidgetBottomBarBinding
import com.trailer.base.view.BView
import java.io.IOException

open class WidgetBottomBar constructor (context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr) {
    lateinit var binding: WidgetBottomBarBinding

    constructor(context: Context?,attrs: AttributeSet?) : this(context, attrs,0)

    private fun initView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_bottom_bar, this, true)

        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
        background = resources.getDrawable(R.drawable.navbar_bg, null)

    }

    private fun listeners() {

        binding.imgBack.setOnClickListener { if (context is BView) (this.context as BView).finishActivity() else (context as Activity).finish() }

        binding.imgHome.setOnClickListener { v: View? ->
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_HOME)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
            context.startActivity(intent)
        }
        binding.imgRecent.setOnClickListener { v: View? ->
            val runtime = Runtime.getRuntime()
            try {
                runtime.exec("input keyevent " + KeyEvent.KEYCODE_MENU)
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }

    }

    init {
        initView(context, attrs, defStyleAttr)
        listeners()
    }
}
